package com.tanhua.dubbo.api.mongo.impl;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.dubbo.api.mongo.RecommendUserApi;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class RecommendUserApiImpl implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public RecommendUser queryWithMaxScore(Long toUserId) {
        Query query = new Query(Criteria.where("toUserId").is(toUserId));
        query.with(Sort.by(Sort.Order.desc("score"))).limit(1);
        RecommendUser bestOne = mongoTemplate.findOne(query, RecommendUser.class);
        return bestOne;
    }

    @Override
    public PageResult queryRecommendUsers(Long toUserId, Integer page, Integer pagesize) {
        Query query = new Query(Criteria.where("toUserId").is(toUserId));
        long counts = mongoTemplate.count(query, RecommendUser.class);
        query.with(Sort.by(Sort.Order.desc("score"))).skip((page - 1) * pagesize).limit(pagesize);
        List<RecommendUser> recommendUserList = mongoTemplate.find(query, RecommendUser.class);
        return new PageResult(page.longValue(), pagesize.longValue(), counts, recommendUserList);
    }

    @Override
    public RecommendUser findById(Long recommendUserId, Long id) {
        Query query = new Query(Criteria.where("userId").is(recommendUserId).and("toUserId").is(id));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if (recommendUser == null) {
            recommendUser.setUserId(recommendUserId);
            recommendUser.setToUserId(id);
            recommendUser.setScore(80d);
        }
        return recommendUser;
    }

    @Override
    public List<RecommendUser> queryCardsList(Long userId) {
        //1.查询出该用户的喜欢不喜欢的userid集合
        Query query1 = new Query(Criteria.where("userId").is(userId));
        List<UserLike> userLikeList = mongoTemplate.find(query1, UserLike.class);
        List<Long> likeUserId = CollUtil.getFieldValues(userLikeList, "likeUserId", Long.class);

        //2.查询出该用户朋友的userid集合
//        Query query2 = new Query(Criteria.where("userId").is(userId));
//        List<Friend> friendList = mongoTemplate.find(query2, Friend.class);
//        List<Long> friendId = CollUtil.getFieldValues(friendList, "friendId", Long.class);

        //3.根据前两个条件查询出推荐列表
        Criteria criteria = Criteria.where("toUserId").is(userId).and("userId").nin(likeUserId);
        TypedAggregation typedAggregation = TypedAggregation.newAggregation(RecommendUser.class,
                Aggregation.match(criteria),Aggregation.sample(10));
        AggregationResults<RecommendUser> aggregate = mongoTemplate.aggregate(typedAggregation, RecommendUser.class);
        return aggregate.getMappedResults();
    }

}
